package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.domain.Spot;

import cn.wolfcode.wolf2w.query.SpotQuery;
import cn.wolfcode.wolf2w.repostiory.SpotRepository;

import cn.wolfcode.wolf2w.service.ISpotService;

import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
//@Transactional  暂时先别管事务
public class SpotServiceImpl implements ISpotService {

    @Autowired
    private SpotRepository repository;
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Spot get(String id) {
        Spot spot = repository.findById(id).orElse(null);

        return spot;
    }

    @Override
    public Page<Spot> query(SpotQuery qo) {
        //查询条件对象
        Query query = new Query();

        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");


        return  DBHelper.query(mongoTemplate,Spot.class,query,pageable);
    }


    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }



    @Override
    public List<Spot> list() {
        return repository.findAll();
    }





}
